<?php
/***************************************************************************
                           download.php  -  description
                           ----------------------------
    begin                : Wed Apr 21 2004
    copyleft             : (C) 2004 Bluemoon inc.
    home page            : http://www.bluemooninc.biz/
    auther               : Yoshi.Sakai
    email                : webmaster@bluemooninc.biz
    Special Thanks to    : Nat Sakimura

    $Id: download.php,v 1.1.9 2004/05/12 13:25:30 Yoshi.S Exp $

 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/
	ini_set("memory_limit","40M");
	include 'header.php';
	include 'pop.ini.php';		//include 'fileup.ini.php';
	include("../../mainfile.php");
	include(XOOPS_ROOT_PATH."/header.php");
	if (!is_object($xoopsUser) && $BlogCNF['guest_dl']==0){
		redirect_header(XOOPS_URL."/user.php",2,"You must signup before download.");
		exit();
	}
	$filename = cnv_mbstr(rawurldecode($_GET['url']));
	$a = strpos($filename,'_');
	$b = strrpos($filename,'/');
	$c = strrpos($filename,'.');
	$dl_filename = substr($filename,$a+1,$c-$a-1);
	$filename = $BlogCNF['uploads'].substr($filename,$b+1);
	if(!file_exists($filename)){
		print("Error - $filename does not exist.");
		return ;
	}
	$size = filesize($filename);
	//print($filename."-".$dl_filename);

	// Modified by Nat Sakimura
	$fname = cnv_mbstr($dl_filename);
	$fnamedotpos = strrpos($fname,'.');
	$fext = substr($fname,$fnamedotpos+1);
	switch($fext) {
	case "pdf":
		$ctype = "application/pdf";
		break;
	case "doc":
		$ctype = "applicaion/msword";
		break;
	case "xls":
		$ctype = "application/excel";
		break;
	case "ppt":
		$ctype = "application/powerpoint";
		break;
	case "jpg":
		$ctype = "image/jpeg";
		break;
	case "txt":
		$ctype = "text/plain";
		break;
	default:
		$ctype = "application/octet-stream-dummy";
		break;
	}
	ob_clean();
	@ignore_user_abort();
	@set_time_limit(0);
	// till here
	
	header("Content-length: " . $size);
	header("Content-disposition: inline; filename=" .  cnv_mbstr($dl_filename));

	// Modified by Nat Sakimura
	header("Content-type: " . $ctype);
	header("Content-Transfer-Encoding: binary");
	header("x-extension: " . $ctype );
	// Modified by Nat Sakimura
	$fp=fopen(cnv_mbstr($filename),'r');
	while(!feof($fp)) {
		$buffer = fread($fp, 1024*6); //speed-limit 64kb/s
		print $buffer;
		flush();
		ob_flush();
		usleep(10000); 
	}
	fclose($fp);
	//
	// Save download log
	//
	if ($xoopsUser) $uname = $xoopsUser->getVar('uname'); else $uname = "Anonymous";
	$str = $uname.",".date("Y-m-d H:i:s", time());
	$postlog = $filename.'.log';
	$fp = fopen($postlog, 'a');
	fwrite($fp, $str."\n");
	fclose($fp);
	//
	// Convert for Multi-byte Strings
	//
	function cnv_mbstr($str) {
		if (extension_loaded('mbstring')){
			return  mb_convert_encoding($str,$BlogCNF['save_as_mbstr'],"auto");
		} else {
			return $str;
		}
	}
?>
